define(['jquery', 'bootstrap', 'frontend', 'form', 'template','echarts'], function ($, undefined, Frontend, Form, Template, Echarts) {
    var validatoroptions = {
        invalid: function (form, errors) {
            $.each(errors, function (i, j) {
                Layer.msg(j);
            });
        }
    };
    var Controller = {
        vocation: function () {
            var a= localStorage.getItem('new-answers');
            var answer_keys = [];
            var answer_arr = [];
            // if(a){
            //     answers = JSON.parse(a);
            //     answer_arr = answers.answer_arr
            //     answer_keys = answers.answer_keys
            //     console.log(answer_keys);
            //     $.each(answer_arr,function (d, v) {
            //         // console.log(d)
            //         // console.log(v)
            //         $.each(v,function (dd, vv) {
            //             // console.log(vv.answer)
            //             // console.log('.subject_'+vv.key +' .answer-'+vv.answer)
            //             $('.subject_'+vv.key +' .answer-'+vv.answer).addClass('h3-selectd');
            //             $(".tm-"+vv.key).removeClass('btn-default');
            //             $(".tm-"+vv.key).addClass('btn-primary');
            //         })
            //     });
            // }
            var count = Config.count;
            var select_count = 0;

            $(document).on('click','.answer',function () {
                var type = $(this).data('type');
                var answer = $(this).data('val');
                var id = $(this).data('id');

                // var b = localStorage.getItem('select_type');
                // if(!b){
                //     localStorage.setItem('select_type',type);
                // }
                //选中颜色
                if(type=='zonghesuzhi'){
                    $('.subject_'+id +' .answer-zonghesuzhi').each(function(index) {
                        var an =  $(this).data('val');
                        if(an == answer){
                            $(this).addClass('btn-info');
                        }else{
                            $(this).removeClass('btn-info');
                        }
                    });
                }
                if(type=='yewunengli'){
                    $('.subject_'+id +' .answer-yewunengli').each(function(index) {
                        var an =  $(this).data('val');
                        if(an == answer){
                            $(this).addClass('btn-info');
                        }else{
                            $(this).removeClass('btn-info');
                        }
                    });
                }
                if(type=='lvzhijinze'){
                    $('.subject_'+id +' .answer-lvzhijinze').each(function(index) {
                        var an =  $(this).data('val');
                        if(an == answer){
                            $(this).addClass('btn-info');
                        }else{
                            $(this).removeClass('btn-info');
                        }
                    });
                }
                var params = $(this).data('params')

                if(answer_arr.length==0){
                    answer_arr.push({id:id,type:params.table_id,answer:answer});
                }else if(answer_arr.length==1){
                    if(answer_arr[0]['type']==type){
                        answer_arr[0]['answer'] = answer;
                    }else{
                        answer_arr.push({id:id,type:params.table_id,answer:answer});
                    }
                }else if(answer_arr.length==2){
                    if(answer_arr[0]['type']==type){
                        answer_arr[0]['answer'] = answer;
                    }else if(answer_arr[1]['type']==type){
                        answer_arr[1]['answer'] = answer;
                    }else{
                        answer_arr.push({id:id,type:params.table_id,answer:answer});
                    }
                }else if(answer_arr.length==3){
                    if(answer_arr[0]['type']==type){
                        answer_arr[0]['answer'] = answer;
                    }else if(answer_arr[1]['type']==type){
                        answer_arr[1]['answer'] = answer;
                    }else if(answer_arr[2]['type']==type){
                        answer_arr[2]['answer'] = answer;
                    }else{
                        answer_arr.push({id:id,type:params.table_id,answer:answer});
                    }
                }
                if(answer_keys.indexOf(id) == -1){
                    answer_keys.push(id)
                }
                select_count = answer_arr.length;
                console.log(answer_arr.length)
                // $.each(answer_arr,function (dd, vv) {
                //     console.log(vv.type+'!='+type);
                //     console.log(vv.id+'@@@'+id);
                //     if(vv.id==id && vv.type!=type){
                //         select_count = select_count+1;
                //     }
                //     // console.log('.subject_'+vv.key +' .answer-'+vv.answer)
                // })
                
                //判断跳转到下一测评
                var mycount= answer_keys.length;
                
                
                console.log(answer_arr);

                //如果是最后一个测评完成后就提交
                if(count == mycount && select_count % 3 == 0){
                    var layerIndex = layer.load(3, {
                        shade: [0.7,'gray'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: "/index/suitable/vocation",
                        type: 'post',
                        dataType: 'json',
                        data: {answer_arr:answer_arr},
                        success: function (ret) {
                            if(ret.code == 0){
                                layer.alert('请刷新重试');
                                layer.close(layerIndex);
                            }else{
                                layer.close(layerIndex);
                                layer.alert('完成测评');
                                window.location.href = "vocation_report.html";
                            }
                        }
                    });
                }else{
                    console.log(select_count+'####');
                    if(select_count % 3 == 0){
                        $('.subject_'+id).hide();
                        $('.subject_'+(id+1)).show();
                        $('.subject_'+(id+1) +' .answer').removeClass('btn-info');
                    }
                }
            })
        },
        
        
        login: function () {
            //本地验证未通过时提示
            $("#login-form").data("validator-options", validatoroptions);

            $(document).on("change", "input[name=type]", function () {
                var type = $(this).val();
                $("div.form-group[data-type]").addClass("hide");
                $("div.form-group[data-type='" + type + "']").removeClass("hide");
                $('#resetpwd-form').validator("setField", {
                    captcha: "required;length(4);integer[+];remote(" + $(this).data("check-url") + ", event=resetpwd, " + type + ":#" + type + ")",
                });
                $(".btn-captcha").data("url", $(this).data("send-url")).data("type", type);
            });

            //为表单绑定事件
            Form.api.bindevent($("#login-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });

            Form.api.bindevent($("#resetpwd-form"), function (data) {
                Layer.closeAll();
            });

            $(document).on("click", ".btn-forgot", function () {
                var id = "resetpwdtpl";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: __('Reset password'),
                    area: ["450px", "355px"],
                    content: content,
                    success: function (layero) {
                        Form.api.bindevent($("#resetpwd-form", layero), function (data) {
                            Layer.closeAll();
                        });
                    }
                });
            });
        },
        register: function () {
            //本地验证未通过时提示
            $("#register-form").data("validator-options", validatoroptions);

            //为表单绑定事件
            Form.api.bindevent($("#register-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });
        },
        changepwd: function () {
            //本地验证未通过时提示
            $("#changepwd-form").data("validator-options", validatoroptions);

            //为表单绑定事件
            Form.api.bindevent($("#changepwd-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });
        },
        profile: function () {
            // 给上传按钮添加上传成功事件
            $("#plupload-avatar").data("upload-success", function (data) {
                var url = Fast.api.cdnurl(data.url);
                $(".profile-user-img").prop("src", url);
                Toastr.success(__('Upload successful'));
            });
            Form.api.bindevent($("#profile-form"));
            $(document).on("click", ".btn-change", function () {
                var that = this;
                var id = $(this).data("type") + "tpl";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: "修改",
                    area: ["400px", "250px"],
                    content: content,
                    success: function (layero) {
                        var form = $("form", layero);
                        Form.api.bindevent(form, function (data) {
                            location.reload();
                            Layer.closeAll();
                        });
                    }
                });
            });
        }
    };
    return Controller;
});